package com.nuatar.meibi.pubvar;

import java.util.ArrayList;
import java.util.List;

import com.nuatar.meibi.R;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

public class LXHListView extends ScrollView {
	LinearLayout mylist;
	Context ctx;
	int Colums=2;
	

	
	public LXHListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		ctx=context;
		init_all();
	}
	public LXHListView(Context context){
		super(context);
		ctx=context;
		init_all();
	}
	private void init_all(){
		List<View> vlist=new ArrayList<View>();
		
		for(int i=0;i<200;i++){
			MyImageButton ivs=new MyImageButton(ctx);
			Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
			ivs.setImageBitmap(bmp);
			vlist.add(ivs);
		}
		LinearLayout.LayoutParams lvparam = new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		
		lvparam.setMargins(0, 0, 0, 0);
		
		mylist=new LinearLayout(ctx);
		mylist.setPadding(0, 0, 0, 0);
		mylist.setLayoutParams(lvparam);
		mylist.setOrientation(LinearLayout.VERTICAL);
		setData(vlist);
		this.addView(mylist);
		this.setBackgroundColor(Color.WHITE);
	}
	public void setData(List<View> ad){
		int seed=(ad.size()%Colums>0)?1:0;
		int count=ad.size()/Colums+seed;
		for(int i=1;i<=count;i++)
		{
			LinearLayout mline=new LinearLayout(ctx);
			mline.setOrientation(LinearLayout.HORIZONTAL);
			LinearLayout.LayoutParams mparam = new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
			LinearLayout.LayoutParams lineparam = new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
			lineparam.setMargins(0, 0, 0, 0);
			mparam.weight=1;
			mparam.setMargins(0, 0, 0, 0);
			mline.removeAllViews();
			mline.setPadding(0, 0, 0, 0);
			mline.setLayoutParams(lineparam);
			for(int n=0;n<Colums;n++){
				RelativeLayout rla=new RelativeLayout(ctx);
				RelativeLayout.LayoutParams rparam= new RelativeLayout.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT,android.view.ViewGroup.LayoutParams.MATCH_PARENT);
				rparam.addRule(RelativeLayout.CENTER_IN_PARENT);
				int dot=(i-1)*Colums+n;
				if(dot<ad.size()){
				View v=ad.get(dot);
				v.setLayoutParams(rparam);
				rla.setLayoutParams(mparam);
				rla.addView(v);
				mline.addView(rla);
				}else
				{
					continue;
				}
			}
			mylist.addView(mline);
			
		}
	}
	public int getColums() {
		return Colums;
	}
	public void setColums(int colums) {
		Colums = colums;
	}
}
